package com.zadtek.shopmall.other;

import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.zadtek.shopmall.App;
import com.zadtek.shopmall.utils.LogUtil;
import com.zadtek.shopmall.utils.ToastUtil;


public class LocationManger {
    public interface OnLocationListener{
        public void onLocation(AMapLocation location);
    }

    public static AMapLocation location;
    private static boolean isFetching;

    public static void fetch(final OnLocationListener listener){
        if(location!=null && location.getErrorCode()==0){
            if(listener!=null) listener.onLocation(location);
        }else{
            AMapLocationClient mLocationClient = new AMapLocationClient(App.getApplication());
            AMapLocationListener mLocationListener = new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation aMapLocation) {
                    if(listener!=null) listener.onLocation(aMapLocation);
                    location = aMapLocation;
                    LogUtil.d("定位调用了");
                }
            };
            AMapLocationClientOption option = new AMapLocationClientOption();
            option.setNeedAddress(true);
            option.setOnceLocation(true);
            option.setOnceLocationLatest(true);
            mLocationClient.setLocationListener(mLocationListener);
            mLocationClient.setLocationOption(option);
            mLocationClient.startLocation();
        }
    }
}
